package com.liehu.sms.listener;

import com.liehu.sms.client.EmployeeClient;
import com.liehu.sms.pojo.Employee;
import com.liehu.sms.pojo.Residence;
import entity.Result;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


import java.util.List;
import java.util.Map;

/**
 * 监听消息队列
 * 消费 employeeList 用于处理redis的队列顺序和队列的有效性
 */
@Component
@RabbitListener(queues = "employeeList")
public class EmployeeListListener {
    @Autowired
    private EmployeeClient employeeClient;

    @Autowired
    private RedisTemplate redisTemplate;//redis
    /**
     * 消费处理，用于处理redis的队列顺序和队列的有效性
     * 用于在分配业务员之后，将该业务员的位置由首位放到末尾
     */
    @RabbitHandler
    public void residence(Map<String,String> map){
        //获取信息
        String state = map.get("state");
        String employeeId = map.get("employeeId");
        String roleId = map.get("roleId");
        //1.先从redis中取出当前的业务员队列
        List<Employee> employeeList = (List<Employee>) redisTemplate.opsForValue().get("employeeList");

        /**
         *  当state为1时，处理队列的顺序
         */
        if(state !=null && state.equals("1")){
            try {

                //1.2.循环遍历，将头部的业务员放在末尾
                for(int i=0;i<employeeList.size();i++){
                    if(i == 0){
                        //1.3如果是首位，就将首位直接添加到末尾
                        employeeList.add(employeeList.get(i));
                    }else{
                        //1.4将集合后一位前移一位
                        employeeList.set(i-1,employeeList.get(i));

                        if(i == employeeList.size()-1){
                            //1.5删除最后一位重复元素
                            employeeList.remove(employeeList.size()-1);
                        }
                    }
                    //1.6.将新的集合重新放入缓存里
                    redisTemplate.opsForValue().set("employeeList",employeeList);

                }
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
        }

        /**
         *  当state为2时，剔除队列中已经失效的员工
         *  当调用用户微服务的离职方法时，将执行此操作
         */
        try {
     if(state !=null && state.equals("2")){

                int num = -1;//用以标识失效的员工的位置，默认-1
                //2.1.循环遍历集合
                for(int i=0;i<employeeList.size();i++){
                    //2.2找到已经失效的员工id的位置
                    if(employeeId.equals(employeeList.get(i).getId())){
                        num = i;//标识位置
                        break;//停止循环
                    }
                }
                //3.1获得了下标后，将该员工剔除
                if(num != -1){
                  for(int i=num;i<employeeList.size();i++){
                    //3.2剔除该员工
                      if(i == num){
                          employeeList.remove(num);
                      }
                  }
                }
              //将新的集合重新放入缓存里
             redisTemplate.opsForValue().set("employeeList",employeeList);

            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        /**
         * 当state为0时，刷新(导入)队列中的数据
         */
        if(state !=null && state.equals("0")){
            if(roleId !=null && !roleId.equals("")){
                //调用方法查询RoleID
                Result clientByRoleId = employeeClient.findByRoleId(roleId);
                //得到集合对象
                List<Employee> listEmployee = (List<Employee>) clientByRoleId.getData();
                if(listEmployee !=null && listEmployee.size() != 0){
                    //存入缓存
                    redisTemplate.opsForValue().set("employeeList",listEmployee);
                }
            }

        }


    }
}
